package com.keur.money.ui.steps.act

import android.graphics.Color
import android.view.View
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.databinding.ActMullaStepsBinding
import com.keur.money.databinding.ItemMullaStepTitleBinding
import com.keur.money.http.HttpHelper
import com.keur.money.mvp.MvpAct
import com.keur.money.ui.steps.fm.KeurStepAccountFm
import com.keur.money.ui.steps.fm.KeurStepCommonFm
import com.keur.money.ui.steps.fm.KeurStepOcrFm
import com.keur.money.ui.steps.fm.KeurStepPhoneBookFm
import com.keur.money.ui.steps.fm.KeurStepSelfieFm
import com.keur.money.ui.steps.impl.KeurStepsPresenterImpl
import com.keur.money.ui.steps.view.IKeurStepsView
import com.keur.money.utils.MullaChoosePhoneBookUtils
import com.keur.money.utils.event.KeurEventData
import com.keur.money.utils.event.KeurEventType
import com.keur.money.utils.helper.KeurCustomConfigsHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/30 13:53
 * @Annotation：认证流程
 */
class KeurStepsAct: MvpAct<IKeurStepsView, KeurStepsPresenterImpl>(),IKeurStepsView {

    private var _binding: ActMullaStepsBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "Cannot access binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )
    //选择联系人
    var pickPhonebook: MullaChoosePhoneBookUtils? = null

    override fun createPresenter(): KeurStepsPresenterImpl {
        return KeurStepsPresenterImpl(this)
    }
    override fun getMullaRootView(): View {
        _binding = ActMullaStepsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initMullaView() {
        setMullaTitleBarVisibility(View.VISIBLE)
        setMullaTitleBarColor(Color.parseColor("#FFF7F5FF"))
        setMullaBackBtnRes(R.mipmap.mulla_back_black)
        setMullaRightBtnRes(R.mipmap.mulla_kefu_icon)
        setMullaTextTitle(getString(R.string.steps_title))

        pickPhonebook = MullaChoosePhoneBookUtils(this)
    }

    override fun initMullaListener() {
        rootViewBinding.ivFun.setOnClickListener {
            val eventRemark = presenter.loadMullaCurrentStepTitle()
            showMullaKefuDialog(eventRemark)
            HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_CUSTOMER,eventRemark)
        }
        //流程认证完成
        _binding?.btnContinuer?.setOnClickListener {
            HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_STEP_COMPLETE_CONTINUER)
            finish()
        }
    }

    override fun initMullaData() {
        presenter.loadMullaProcessStepAction()

        //牌照信息
        val licenseInfo = KeurCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.visibility = View.GONE
        } else {
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.addView(licenseView())
            _binding?.licenseLayout?.visibility = View.VISIBLE
        }
    }

    /**
     * 提交
     */
    fun onMullaSubmitProcessAction(path: String,params: JSONObject){
        showMullaLoading(isTOuchOut = false, isCancel = true)
        presenter.onMullaSubmitProcessAction(path,params)
    }

    /**
     * 设置当前流程进度
     */
    override fun showMullaStepsProgress(max: Int, progress: Int) {
        _binding?.progressBar?.max = max
        _binding?.progressBar?.progress = progress
    }

    /**
     * 移除所有View
     */
    override fun removeMullaStepAllViews() {
        _binding?.selfieTipsLayout?.visibility = View.GONE
        _binding?.afterLayout?.removeAllViews()
        _binding?.afterLayout?.visibility = View.GONE
        _binding?.contentLayout?.removeAllViews()
        _binding?.beforeLayout?.removeAllViews()
        _binding?.beforeLayout?.visibility = View.GONE
        _binding?.btnContinuer?.visibility = View.GONE
    }

    /**
     * 已完成的步骤
     */
    override fun addMullaStepAfterView(stepTitle: String) {
        val itemView = ItemMullaStepTitleBinding.inflate(layoutInflater)
        itemView.textStepTitle.text = stepTitle

        itemView.stepLayout.setBackgroundColor(Color.parseColor("#FF8027FF"))
        itemView.textStepTitle.setTextColor(Color.parseColor("#FFFFFFFF"))
        itemView.imgStepStatus.setImageResource(R.mipmap.mulla_step_after_icon)

        _binding?.afterLayout?.addView(itemView.root)
        _binding?.afterLayout?.visibility = View.VISIBLE
    }

    /**
     * 未开始的步骤
     */
    override fun addMullaStepBeforeView(stepTitle: String) {
        val itemView = ItemMullaStepTitleBinding.inflate(layoutInflater)
        itemView.textStepTitle.text = stepTitle

        itemView.stepLayout.setBackgroundColor(Color.parseColor("#FFF6F6F6"))
        itemView.textStepTitle.setTextColor(Color.parseColor("#FFE0DBEE"))
        itemView.imgStepStatus.setImageResource(R.mipmap.mulla_step_before_icon)

        _binding?.beforeLayout?.addView(itemView.root)
        _binding?.beforeLayout?.visibility = View.VISIBLE
    }

    /**
     * 所有步骤已完成
     */
    override fun showMullaStepsCompletePage() {
        _binding?.btnContinuer?.visibility = View.VISIBLE
    }

    /**
     * 通用样式
     */
    override fun showMullaStepCommonPage(stepItem: JSONObject) {
        val commonFm = KeurStepCommonFm(stepItem)
        replaceContainerFragement(R.id.contentLayout,commonFm)
    }

    /**
     * 联系人
     */
    override fun showMullaStepPhoneBookPage(stepItem: JSONObject) {
        val phonebookFm = KeurStepPhoneBookFm(this,stepItem)
        replaceContainerFragement(R.id.contentLayout,phonebookFm)
    }

    /**
     * Ocr
     */
    override fun showMullaStepOcrPage(stepItem: JSONObject) {
        val ocrFm = KeurStepOcrFm(stepItem)
        replaceContainerFragement(R.id.contentLayout,ocrFm)
    }

    /**
     * 自拍
     */
    override fun showMullaStepSelfiePage(stepItem: JSONObject) {
        //自拍照需要显示顶部提示
        _binding?.selfieTipsLayout?.visibility = View.VISIBLE
        val selfierFm = KeurStepSelfieFm(stepItem)
        replaceContainerFragement(R.id.contentLayout,selfierFm)
    }

    /**
     * 法语地区添加联系人
     */
    override fun showMullaStepAccountPage(stepItem: JSONObject) {
        val accountFm = KeurStepAccountFm(stepItem)
        replaceContainerFragement(R.id.contentLayout,accountFm)
    }

    /**
     * 流程提交成功
     * 需要发送通知刷新总流程
     */
    override fun showMullaSubmitSuccess() {
        //通知刷新总流程
        MyApp.app.publisher.publish(KeurEventData(KeurEventData.TYPE.KEUR_PROCESS_REFRESH))
    }

    override fun onKeurEvent(event: KeurEventData) {
        super.onKeurEvent(event)
        if(event.eventType == KeurEventData.TYPE.KEUR_PROCESS_REFRESHED){//总流程刷新完成
            presenter.loadMullaProcessStepAction()
        }
    }

    override fun onStop() {
        super.onStop()
        _binding?.root?.clearFocus()
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }
}